//获取当前时间（XXXX-XX-XX）
export function getCurDate() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    return year + "-" + month + "-" + day;
}

//向sessionStorage中存储一个JSON对象
export function setSessionStorage(keyStr, value) {
    sessionStorage.setItem(keyStr, JSON.stringify(value));
}

//从sessionStorage中获取一个JSON对象（取不到时返回null）
export function getSessionStorage(keyStr) {
    var str = sessionStorage.getItem(keyStr);
    if (str == '' || str == null || str == 'null' || str == undefined) {
        return null;
    } else {
        return JSON.parse(str);
    }
}

//从sessionStorage中移除一个JSON对象
export function removeSessionStorage(keyStr) {
    sessionStorage.removeItem(keyStr);
}

//向localStorage中存储一个JSON对象
export function setLocalStorage(keyStr, value) {
    localStorage.setItem(keyStr, JSON.stringify(value));
}

//从localStorage中获取一个JSON对象（取不到时返回null）
export function getLocalStorage(keyStr) {
    var str = localStorage.getItem(keyStr);
    if (str == '' || str == null || str == 'null' || str == undefined) {
        return null;
    } else {
        return JSON.parse(str);
    }
}

//从localStorage中移除一个JSON对象
export function removeLocalStorage(keyStr) {
    localStorage.removeItem(keyStr);
}


export function alert_res(res) {
    let _this = this
    console.log(res)
    if (res === null || res === undefined) {
        return;
    }
    if (res.status === 200 || res.status === '200') {
        _this.$notify({
            title: '成功',
            message: res.msg,
            type: 'success',
            duration: 2000
        });
    } else if (res.status !== 200) {
        _this.$notify({
            title: '失败',
            message: res.msg,
            type: 'error',
            duration: 2000
        });
    }
}

//alert 信息
export function alert_msg(msg) {
    this.$message(msg.toString());
}

//封装的请求   第一个参数是url  第二个参数是data 如果没有data(请求的参数)就直接传null 第三个传type 表示请求的类型
export async function ajax_(url, data, type) {
    try {
        let res
        // console.log(data)
        if (data === null) {
            if (type === "post") {
                res = (await this.$axios.post(url)).data
            } else if (type === "get") {
                res = (await this.$axios.get(url)).data
            } else if (type === "put") {
                res = (await this.$axios.put(url)).data
            } else if (type === "delete") {
                res = (await this.$axios.delete(url)).data
            }
        } else {
            // console.log(data)
            if (type === "post") {
                res = (await this.$axios.post(url, data)).data
            } else if (type === "get") {
                res = (await this.$axios.get(url, data)).data
            } else if (type === "put") {
                res = (await this.$axios.put(url, data)).data
            } else if (type === "delete") {
                res = (await this.$axios.delete(url, data)).data
            }

        }
        return res
    } catch (e) {
        this.$alert_msg("网络开小差了~请稍后再试")
        return null
    }
}